package com.deng.maintenance.dao.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import com.deng.maintenance.dao.IKPIValue;
import com.deng.maintenance.data.MaintenanceData;
import com.deng.maintenance.vo.KPIValueVOOld;
import com.deng.maintenance.vo.KPI_DicVO;
import com.deng.maintenance.vo.Stat_OrgVO;

public class KPIValueImpl implements IKPIValue {
	
	public List<Stat_OrgVO> searchSataOrg(String newCode) {
		// TODO Auto-generated method stub
		System.out.println("-------------KPIValueImpl.searchSataOrg--------------------------");
		MaintenanceData da=new MaintenanceData();
		return da.searchSataOrg(newCode,"1");
	}
	
	public List<KPIValueVOOld> searchKPIValues(String dep_id,String duty_id,String year,String fre_code,String type){
		
		System.out.println("---------------KPIValueImpl.searchKPIValues----------------------");
		List<KPIValueVOOld> ret=new ArrayList<KPIValueVOOld>();
		
		MaintenanceData da=new MaintenanceData();
		ret= da.searchKPIValues(dep_id,duty_id,year, fre_code,type);
		
		
		List<KPIValueVOOld> list=da.searchKpi_Dic(dep_id, duty_id, fre_code, year, type);
		
		ret.addAll(list);
		
		return ret;
	}
	
	public String saveKPIValues(List<KPIValueVOOld> vos){
		System.out.println("---------------KPIValueImpl.saveKPIValues----------------------");
		String ret="succeed";
		if(vos!=null){
			List<KPIValueVOOld> insert=new ArrayList<KPIValueVOOld>();
			List<KPIValueVOOld> update=new ArrayList<KPIValueVOOld>();
			for(int i=0;i<vos.size();i++){
				KPIValueVOOld vo=(KPIValueVOOld)vos.get(i);
				if(vo.getId()==-1){
					vo.setId(null);
					insert.add(vo);
				}
				else{
					update.add(vo);
				}
			}
			MaintenanceData da=new MaintenanceData();
			if(insert.size()>0){
				ret=da.insertKPIValues(insert);
			}
			if(update.size()>0){
				ret=da.updateKPIValues(update);
			}
		}
		return ret;
	}
	
	public String  importKPIValueByExcel(String dep_id ,File file){
		String ret="succeed";
		try {
			Workbook wb=Workbook.getWorkbook(file);
			Sheet sh=wb.getSheet(0);
			int rownum=sh.getRows();
			System.out.println("row="+rownum);
			for(int i=2;i<rownum;i++){
				
				Cell ce_input=sh.getCell(7, i);
				String input=ce_input.getContents();
				if(input==null||"".equals(input)){
					continue;
				}
				Cell ce_id=sh.getCell(0, i);
				Cell ce_freq=sh.getCell(2,i);
				Cell ce_year=sh.getCell(5, i);
				Cell ce_time=sh.getCell(6, i);
				
				String id=ce_id.getContents();
				if(id==null||"".equals(id)){
					ret="'KPI序号'不能为空！！";
					return ret ;
				}
				String freq=ce_freq.getContents();
				String year=ce_year.getContents();
				if(year==null||"".equals(year)){
					ret="'年'不能为空！！";
					return ret ;
				}
				String time=ce_time.getContents();
				
				
				MaintenanceData da=new MaintenanceData();
				List<KPI_DicVO> list=da.searchKpi_Dic(Integer.valueOf(id), dep_id);
				if(list.size()==0){
					ret="'KPI序号'不存在，为："+id;
					return ret;
				}
				KPI_DicVO kpi_dic=list.get(0);
				
				
				KPIValueVOOld vo=new KPIValueVOOld();
				vo.setDep_id(dep_id);
				vo.setKpi_id(Integer.valueOf(id));
				vo.setStatus("0");
				vo.setDuty_id(4);
				vo.setKpi_input_value(input);
				
				if("年度".equals(freq)){
					vo.setFrq_code("0001");
					
				}
				else if("半年度".equals(freq)){
					vo.setFrq_code("0002");
					
					if(time==null||"".equals(time)){
						ret="'KPI序号'为"+id+",'时间'不能为空！！";
						return ret ;
					}
					
					if("上半年".equals(time)){
						vo.setHalf_year("up");
					}
					else if("下半年".equals(time)){
						vo.setHalf_year("down");
					}
					else{
						ret="'KPI序号'为"+id+",'时间'值错误，请参考excel 顶部的说明！！";
						return ret ;
					}
				}
				else if("季度".equals(freq)){
					vo.setFrq_code("0003");	
					if(time==null||"".equals(time)){
						ret="'KPI序号'为"+id+",'时间'不能为空！！";
						return ret ;
					}
					if("一季度".equals(time)){
						vo.setQuarter("1");
					}
					else if("二季度".equals(time)){
						vo.setQuarter("2");
					}
					else if("三季度".equals(time)){
						vo.setQuarter("3");
					}
					else if("四季度".equals(time)){
						vo.setQuarter("4");
					}
					else{
						ret="'KPI序号'为"+id+",'时间'值错误，请参考excel 顶部的说明！！";
						return ret ;
					}
				}
				else if("月".equals(freq)){
					vo.setFrq_code("0004");
					if(time==null||"".equals(time)){
						ret="'KPI序号'为"+id+",'时间'不能为空！！";
						return ret ;
					}
					if("1月".equals(time)){
						vo.setMonth("1");
					}
					else if("2月".equals(time)){
						vo.setMonth("2");
					}
					else if("3月".equals(time)){
						vo.setMonth("3");
					}
					else if("4月".equals(time)){
						vo.setMonth("4");
					}
					else if("5月".equals(time)){
						vo.setMonth("5");
					}
					else if("6月".equals(time)){
						vo.setMonth("6");
					}
					else if("7月".equals(time)){
						vo.setMonth("7");
					}
					else if("8月".equals(time)){
						vo.setMonth("8");
					}
					else if("9月".equals(time)){
						vo.setMonth("9");
					}
					else if("10月".equals(time)){
						vo.setMonth("10");
					}
					else if("11月".equals(time)){
						vo.setMonth("11");
					}
					else if("12月".equals(time)){
						vo.setMonth("12");
					}
					else{
						ret="'KPI序号'为"+id+",'时间'值错误，请参考excel 顶部的说明！！";
						return ret ;
					}
					
				}
				else{
					ret="‘考核频率’错误，为‘"+freq+"'";
					return ret;
				}
				vo.setYear(Integer.valueOf(year));
				
				KPTSettingImpl kpiset=new KPTSettingImpl();
				
				vo.setKpi_score(kpiset.count(Integer.valueOf(id), input));
				
				List<KPIValueVOOld> tmplist=da.searchKPIValues(vo);
				if(tmplist.size()==0){
					List<KPIValueVOOld> tmp=new ArrayList<KPIValueVOOld>();
					tmp.add(vo);
					da.insertKPIValues(tmp);
				}
				else{
					KPIValueVOOld tmpvo=tmplist.get(0);
					if(tmpvo.getStatus()=="0"){
						vo.setId(tmpvo.getId());
						vo.setCreate_time(tmpvo.getCreate_time());
						List<KPIValueVOOld> tmp=new ArrayList<KPIValueVOOld>();
						tmp.add(vo);
						da.updateKPIValues(tmp);
					}
					else{
						ret+=" 'KPI序号'为"+id+",已经审核，不能导入！！";
					}
				}
				
			}
			
			
			
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}
	
	public static void main(String arg[]){
		KPIValueImpl temp=new KPIValueImpl();
		System.out.println(temp.importKPIValueByExcel("5",new File("E:\\work\\保卫科.xls")));
	}

}
